package collect.taobao.util.generate;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class FileReader {
	private BufferedReader br = null;
	private BufferedReader br2 = null;
	private String line = null;
	private LinkedList<String> ll = new LinkedList<String>();
	private File file = null;
	private int lineNum;
	
	public FileReader() {};
	
	public FileReader(File file){
		this.file = file;
		init();
	}
	
	public FileReader(String file) {
		this.file = new File(file);
		init();
	}
	
	private void init() {
		try {
			br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	/**
	 * �õ�һ��BufferReader�������
	 * @return
	 */
	public BufferedReader getBR() {
		return br;
	}
	
	/**
	 * �õ�һ���ļ��е�ָ��ǰcount��
	 * @param count ��ȡ�ļ�������,��1��ʼ
	 * @param hasNum �Ƿ���ʾ����
	 * @return
	 */
	public String getLine(int count,boolean hasNum) {
		br2 = getNewBR();
		StringBuffer sb = new StringBuffer();
		int i = 0;
		try {
			while((line=br2.readLine())!=null) {
				i++;
				if(i<=count) {
					if(hasNum) {
						sb.append(i+"\t");
					}
					sb.append(line+"\r\n");
				}else {
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(br2);
		}
		return sb.toString();
	}
	
	/**
	 * �õ�ָ��start����ʼ��count������
	 * @param start ��ʼ��ȡ��λ�ã���1��ʼ
	 * @param count ��ȡ������
	 * @param hasNum �Ƿ���ʾ����
	 * @return
	 */
	public String getLine(int start,int count,boolean hasNum) {
		br2 = getNewBR();
		StringBuffer sb = new StringBuffer();
		int i = 0;
		try {
			while((line=br2.readLine())!=null) {
				i++;
				if(i<start) continue;
				if(i<=count+start) {
					if(hasNum) {
						sb.append(i+"\t");
					}
					sb.append(line+"\r\n");
				}else {
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(br2);
		}
		return sb.toString();
	}
	
	/**
	 * �õ��ļ���������
	 * @return
	 */
	public long getLineNum() {
		long size = 0L;
		br2 = getNewBR();
		try {
			while((line=br2.readLine())!=null) {
				size++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(br2);
		}
		return size;
	}
	/**
	 * �õ�����count������
	 * @param count
	 * @param hasNum �Ƿ���ʾ����
	 * @return
	 */
	public String getLastLine(int count,boolean hasNum) {
		br2 = getNewBR();
		StringBuffer sb = new StringBuffer();
		int i = 0;
		long size = getLineNum();
		
		try {
			while((line=br2.readLine())!=null) {
				i++;
				if(i>=size-count) {
					if(hasNum) {
						sb.append(i+"\t");
					}
					sb.append(line+"\r\n");
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(br2);
		}
		return sb.toString();
	}
	
	/**
	 * ������һ��
	 * @return
	 */
	public String getNextLine() {
		try {
			return br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * ����һ���µ��ļ���ȡ��
	 * @return
	 */
	public BufferedReader getNewBR() {
		try {
			if(br2 != null) close(br2);
			
			br2 = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return br2;
	}
	
	/**
	 * �õ�ָ������һ������
	 * @param count ����
	 * @param hasNum �Ƿ���ʾ����
	 * @return
	 */
	public String getNextLine(int count,boolean hasNum) {
		
		StringBuffer sb = new StringBuffer();
		int i = 0;
		System.out.println("*********************************"+i);
		try {
			while((line=br.readLine())!=null) {
				i++;
				if(i<=count) {
					if(hasNum) sb.append(lineNum+i+"\t");
					sb.append(line+"\r\n");
				}else break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lineNum += i-1;
		return sb.toString();
	}
	
	/**
	 * �õ�ָ������һ������
	 * @param count ����
	 * @return
	 */
	public String getNextLine(int count) {
		return getNextLine(count,false);
	}
	
	/**
	 * �õ����е���,���⣺�˷���ֻ������С�ļ��Ĳ���
	 * @param hasNum �Ƿ���ʾ����
	 * @return
	 */
	public LinkedList<String> getAllLine(boolean hasNum) {
		br2 = getNewBR();
		try {
			while((line=br.readLine())!=null) {
				ll.addLast(line+"\r\n");
			}
		}  catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close(br2);
		return ll;
	}
	
	/**
	 * �ر�ָ����BufferedReader
	 * @param br2
	 */
	public void close(BufferedReader br2) {
		if(br2!=null) {
			try {
				br2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * �ر�Ĭ�ϵ�BufferedReader
	 */
	public void close() {
		if(br!=null) {
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
